package com.ruoyi.resident.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.domain.Task;
import com.ruoyi.resident.domain.Resident;
import com.ruoyi.resident.service.IResidentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 住户Controller
 * 
 * @author ruoyi
 * @date 2023-04-07
 */
@RestController
@RequestMapping("/resident")
public class ResidentController extends BaseController
{
    @Autowired
    private IResidentService residentService;

    /**
     * 查询住户列表
     */
    @RequiresPermissions("resident:resident:list")
    @GetMapping("/list")
    public TableDataInfo list(Resident resident)
    {
        startPage();
        List<Resident> list = residentService.selectResidentList(resident);
        return getDataTable(list);
    }

    /**
     * 导出住户列表
     */
    @RequiresPermissions("resident:resident:export")
    @Log(title = "住户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Resident resident)
    {
        List<Resident> list = residentService.selectResidentList(resident);
        ExcelUtil<Resident> util = new ExcelUtil<Resident>(Resident.class);
        util.exportExcel(response, list, "住户数据");
    }

    /**
     * 获取住户详细信息
     */
    @RequiresPermissions("resident:resident:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(residentService.selectResidentById(id));
    }

    @GetMapping(value = "/resident/{id}")
    public Resident residentGetInfo(@PathVariable("id") Long id)
    {
        return residentService.residentGetInfo(id);
    }

    /**
     * 通过楼栋id获取楼栋所有的住户
     * @author kabuda
     * @date 2023/4/11 9:27
     * @return Resident
    */

    @PostMapping("/residentByBuildingId")
    public List<Resident> residentByBuildingId(@RequestBody Task task)
    {
        return residentService.residentByBuildingId(task);
    }
    /**
     * 新增住户
     */
    @RequiresPermissions("resident:resident:add")
    @Log(title = "住户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Resident resident)
    {
        return toAjax(residentService.insertResident(resident));
    }

    /**
     * 修改住户
     */
    @RequiresPermissions("resident:resident:edit")
    @Log(title = "住户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Resident resident)
    {
        return toAjax(residentService.updateResident(resident));
    }

    /**
     * 删除住户
     */
    @RequiresPermissions("resident:resident:remove")
    @Log(title = "住户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(residentService.deleteResidentByIds(ids));
    }
}
